<template>
    <rx-layout>
        <div slot="center">
            <a-card>
                <div style="text-align: right;padding-bottom: 10px;">
                    <a-button type="primary" @click="handleSave">
                        {{$ti18('rxForm.saveBtn','保存')}}
                    </a-button>
                </div>
                <a-form-model ref="form" :model="mdl" :rules="rules">
                    <a-row>
                        <a-col :span="24">
                            <component ref="compomentsValid"
                                       :dataJson="dataConfig"
                                       @mdlChange="mdlChange"
                                       :is="currentComponet">
                            </component>
                        </a-col>
                    </a-row>
                </a-form-model>
            </a-card>
        </div>
    </rx-layout>
</template>
<script>
    import {RxLayout, BaseFormModel, Util} from "@lowcode/jpaas-common-lib";


    import PlatformsDefault from './defComponents/PlatformsDefault';
    import PlatformsWeChat from './defComponents/PlatformsWeChat';
    import PlatformsWeChatGzh from './defComponents/PlatformsWeChatGzh';
    import Platformsfeishu from './defComponents/Platformsfeishu';
    import Platformsdingding from './defComponents/Platformsdingding';
    import PlatFormApi from '@/api/user/org/platForms';

    export default {
        name: 'PlatformsEdit',
        mixins: [BaseFormModel],
        components: {
            Util,
            RxLayout,
            PlatformsDefault,
            PlatformsWeChat,
            PlatformsWeChatGzh,
            Platformsfeishu,
            Platformsdingding
        },
        props: {
            //必须
            layerid: {
                type: String
            },
            //必须写
            destroy: {
                type: Function
            },
            platforms: {
                type: Array
            },
            typeId: {
                type: String
            }
        },
        data() {
            return {
                labelCol: {span: 3},
                wrapperCol: {span: 21},
                rules: {
                    fileSystem: [{required: false, message: this.i18n('form.enterType','请输入文件服务类型','input'), trigger: 'change'}],
                },
                dataObj: {},
                dataJson: {
                    platformswx: {},
                    platformsdingding: {},
                    platformsfeishu: {},
                },
                currentComponet: PlatformsWeChat,
                fileSystem: "PlatformsWeChat",
                platformsTypeList: [],
                isInit: true,
                dataConfig: {}
            }

        },
        created() {
            this.getDataConFig();
            this.setPlatformsTypeList();
            this.fileSystem = "Platforms" + this.typeId;
            this.defChangeType();
        },
        methods: {
            i18n(name,text,key){
                return this.$ti18(name,text,"platForms",key);
            },
            getDataConFig() {
                if (this.platforms.length == 0) {
                    return;
                }
                let obj = this.platforms.find(row => {
                    return this.typeId == row.type;
                });
                if (!obj) {
                    return;
                }
                this.dataConfig = obj.setting;
            },
            mdlChange(key, obj) {
                this.dataJson[key] = obj;
            },
            defChangeType() {
                if (!this.fileSystem) {
                    this.fileSystem = "PlatformsWeChat";
                }
                this.currentComponet = this.fileSystem;
            },
            setPlatformsTypeList() {
                this.platformsTypeList.push({label: this.i18n('form.weCom','企业微信'), value: "PlatformsWeChat"});
                this.platformsTypeList.push({label: this.i18n('form.dingTalk','钉钉'), value: "Platformsdingding"});
                this.platformsTypeList.push({label: this.i18n('form.lark','飞书'), value: "Platformsfeishu"});
            },
            handleSave() {
                var self_ = this;
                var callback = function (data) {
                    self_.okSaveFun(data);
                };
                this.$refs.compomentsValid.checkAndSave(callback);
            },
            async okSaveFun(data) {
                var self_=this;
                //进行数据保存后返回填写的内容
                var saveData={
                    id:this.getPkId(),
                    type:this.typeId,
                    setting:JSON.stringify(data)
                };
                var res =await PlatFormApi.save(saveData);
                if(!res.success){
                    self_.$message.error(res.message);
                    return;
                }
                var updateObj={
                    pkId:res.data.id,
                    setting:data
                }
                Util.closeWindow( this, 'ok', updateObj);
                return;
            },
            getPkId() {
                var saveData=this.platforms;
                if (saveData.length == 0) {
                    return "";
                }
                let obj = saveData.find(row => {
                    return this.typeId == row.type;
                });
                if (!obj) {
                    return "";
                }
                return obj.pkId;
            },

        }
    }
</script>
<style>
    .relick a {
        margin-right: 10px;
    }

</style>